package zouran.filepractice.service;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @author zouran
 * createDate：2023/5/8  0:50
 */
public class ExcelRead {
    //resources下的相对路径
    public final String relativeUrl="\\study\\excel.xlsx";
    //拼接文件读取路径
    private final String path=new File(Objects.requireNonNull(this.getClass().getResource("/")).getPath()+relativeUrl).getPath();
    @Test
  public void read() throws IOException {
//      String path="D:\\study\\excel.xlsx";
      InputStream inputStream= Files.newInputStream(Paths.get(path));
      Workbook workbook=new XSSFWorkbook(inputStream);
      Sheet sheet= workbook.getSheetAt(0);
      for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
          for (int j = 0; j < 2; j++) {
             System.out.print(sheet.getRow(i).getCell(j)+"\t");
          }
          System.out.println();
      }
  }
}
